package com.sjgtw.cloud.util;

import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;
import com.mapbox.turf.TurfConstants;
import com.mapbox.turf.TurfMisc;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class TransformUtil {

    public static Point transformSectionToLonLat(LineString line, String kmInt, String kmFloat) {
        // 计算长度
        double length = Double.parseDouble(kmInt) + Double.parseDouble(kmFloat) * Math.pow(0.1, 3);

        double start = 0.0;
        double stop = length;

        if (stop > 0) {
            // 使用Turf库进行lineSliceAlong操作
            LineString res = TurfMisc.lineSliceAlong(line, start, stop, TurfConstants.UNIT_KILOMETERS);

            // 获取最后一个坐标
            Point lonlat = res.coordinates().get(res.coordinates().size() - 1);
            return lonlat;
        } else {
            // 获取第一个坐标
            Point lonlat = line.coordinates().get(0);
            return lonlat;
        }
    }

    public static String readFile(InputStream fis) {
        String jsonStr = "";
        try {
            Reader reader = new InputStreamReader(fis, "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
